<template>
  <div 
    class="status" 
    element-loading-text="数据加载中"
    element-loading-spinner="el-icon-loading"
    v-loading="loading"
  >
    <div class="coverTitle">{{ buName }}{{ dcPeriod }}季度数字化覆盖度现状</div>
    <div v-if="isNoData" class="noData">
      <div>暂无审核通过的数据</div>
    </div>
    <div v-else>
      <div class="btn_group">
        <div class="tip">
          <div class="content">
            <div>数字化应用覆盖深度:{{ depth }}</div>
            <div>上期: {{ previousDepth }}</div>
          </div>
          <div class="content">
            <div>数字化应用覆盖广度:{{ scope }}</div>
            <div>上期:{{ previousScope }}</div>
          </div>
          <div class="content">
            <div>关键业务环节数字化覆盖度: {{ keyLink }}</div>
            <div>上期: {{ previousKeyLink }}</div>
          </div>
        </div>
        <a class="filename" v-for="file in attachment" :key="file.id" @click="downLoad(file)">
          附件: {{ file.name }}
        </a>
        <div class="btn_list" v-if="!approve">
          <RPButton optType="create" type="info" @click="exportHandle" text="导出" />
          <RPButton :disabled="isShow" optType="create" @click="flowHandle" text="查看流程" />
          <RPButton :disabled="isShow" optType="create" @click="approvalHandle" text="查看审批记录" />
        </div>
      </div>
      <div class="versionCompare">
        <div class="text">版本对比</div>
        <Tooltip placement="right-start" max-width="1000" theme="light" class="tooltip" transfer>
          <Icon type="ios-notifications" size="24" />
          <template #content>
            <div style="max-height: 500px; overflow: auto;white-space: normal;">
              <div v-if="!changelog.length">无变化</div>
              <div v-else v-for="(item, index) in changelog" :key="index">{{ item }}</div>
            </div>
          </template>
        </Tooltip>
        <Tooltip placement="right-start">
          <Icon type="ios-help-circle-outline" size="24" />
          <template #content>
            <div style="height: 160px;">
              <p>标示说明</p>
              <p class="tip_content"><i class="block"></i>建设情况发生变化</p>
              <p class="tip_content"><Icon size="20" type="md-checkmark" color="#00C600"></Icon>已上线</p>
              <p class="tip_content"><Icon size="20" type="md-radio-button-off" color="#B85913"></Icon>规划/建设中</p>
              <p class="tip_content"><Icon size="20" type="md-remove" color="#979797"></Icon>不涉及</p>
            </div>
          </template>
        </Tooltip>
      </div>
      <StatusTable
        :columns="columns"
        :data="tableData"
        :total="total"
        :fgState="fgState"
        :height="height"
        @changePage="changePage"
      ></StatusTable>
    </div>
  </div>
</template>

<script>
import coverApi from '@/api/cover'
import StatusTable from '../components/Table.vue'
import suggest from './suggest.vue'
import cover2Api from '@/api/cover2'
import {
  addCategoryHeader,
  flattenData,
  getMaxLevelHeader,
  getColumnSumData,
  addNewChildrenToLowestLevel
} from '../utils/util'
import { handleExport } from '../utils/export'
export default {
  components: {
    StatusTable
  },
  props: {
    approve: {
      type: Boolean,
      default: false
    },
    buCode: {
      type: String,
      default: ''
    },
    dcPeriod: {
      type: String,
      default: ''
    },
    height: {
      type: Number,
      default: 342
    },
    needPermission: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      loading: false,
      buName: '',
      fgState: '',
      columns: [],
      tableData: [],
      total: 0,
      depth: '',
      keyLink: '',
      scope: '',
      previousDepth: '',
      previousKeyLink: '',
      previousScope: '',
      processId: '',
      isShow: false,
      attachment: [], // 附件
      changelog: [] // 版本变化
    }
  },
  computed: {
    isNoData() {
      if (!this.needPermission) {
        return false
      }
      return this.fgState !== '审核通过' ? true : false
    }
  },
  methods: {
    exportHandle() {
      // 简单深拷贝表头
      let headers = JSON.parse(JSON.stringify(this.columns))
      const dealData = flattenData(this.tableData)
      const sumData = getColumnSumData(this.tableData)
      // 表头圈圈ICON
      headers = addNewChildrenToLowestLevel(headers, sumData)
      const firstHeader = getMaxLevelHeader(dealData)
      const sort = addCategoryHeader(headers)
      headers.unshift(firstHeader, sort)
      const sheetName = this.buName + this.dcPeriod + '季度数字化覆盖度现状'
      handleExport(headers, dealData, sheetName)
    },
    async init(currentPage = 1, pageSize = 1000) {
      let self = this
      this.loading = true
      let params = {
        buCode: this.buCode,
        dcPeriod: this.dcPeriod,
        refPreviousData: false,
        currentPage,
        pageSize
      }
      cover2Api
        .getStatusById(params)
        .then(res => {
          this.attachment = JSON.parse(res.data.data.templateConfig?.attachment || '[]')
          this.changelog = res.data.data.changelog || []
          this.tableData = res.data.data.data
          this.fgState = res.data.data.fgState
          this.columns = res.data.data.tableHeaderData
          this.total = res.data.data.total
          this.depth = (res.data.data.depth * 100).toFixed(2) + '%' || 0
          this.keyLink = (res.data.data.keyLink * 100).toFixed(2) + '%' || 0
          this.scope = (res.data.data.scope * 100).toFixed(2) + '%' || 0
          this.previousDepth = (res.data.data.previousDepth * 100).toFixed(2) + '%' || 0
          this.previousKeyLink = (res.data.data.previousKeyLink * 100).toFixed(2) + '%' || 0
          this.previousScope = (res.data.data.previousScope * 100).toFixed(2) + '%' || 0
          this.processId = res.data.data.processId
          this.isShow = res.data.data.processId ? false : true
          this.loading = false
        })
        .catch(() => {
          this.fgState = ''
          this.loading = false
        })
    },
    flowHandle() {
      cover2Api.getProcessUrl({ processInstId: this.processId }).then(res => {
        const url = res.data
        window.open(url, '_blank')
      })
    },
    approvalHandle() {
      new this.$sidelipPage(suggest, {
        props: {
          processInstId: this.processId
        }
      })
    },
    changePage(currentPage, pageSize) {
      this.init(currentPage)
    },
    downLoad(file) {
      coverApi.plantaskDownloadFile(file.fileId)
    }
  },

  created() {
    let bisUnitList = []
    cover2Api.getBuList().then(res => {
      bisUnitList = res.data.data
      bisUnitList.forEach(bu => {
        if (bu.key === this.buCode) {
          this.buName = bu.value
        }
      })
    })
    this.init()
  }
}
</script>

<style lang="less" scoped>
.status {
  .coverTitle {
    font-size: 18px;
    font-weight: 700;
    padding: 10px;
  }

  .noData {
    display: flex;
    height: 500px;
    align-items: center;
    justify-content: center;
  }
  
  .btn_group {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .tip {
      display: flex;
      .content {
        display: flex;
        flex-direction: column;
        margin: 0 10px;
        > :nth-of-type(1) {
          font-size: 16px;
        }
        > :nth-child(2) {
          font-size: 12px;
          color: #ccc;
        }
      }
    }
    .filename {
      margin-right: 20px;
    }
    .btn_list {
      button {
        margin-right: 10px;
      }
    }
  }
  .versionCompare {
    display: flex;
    margin-top: 10px;
    .text {
      display: flex;
      font-size: 18px;
      font-weight: 700;
      margin-right: 20px;
      &::before {
        display: block;
        content: '';
        width: 6px;
        height: 26px;
        background: #f89c34;
        margin-right: 4px;
      }
    }
    .tooltip {
      margin: 0 15px;
    }
    .tip_content {
      display: flex;
      align-items: center;
      margin: 10px 0;
      i {
        width: 30px;
        height: 26px;
        padding-top: 3px;
        border: 1px solid #eee;
        margin-right: 10px;
      }
      .block {
        width: 30px;
        height: 15px;
        background: #ffb77c;
      }
    }
  }
}
</style>
